package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbAddressMapper;
import com.guigu.qingcheng.pojo.TbAddress;
import com.guigu.qingcheng.service.TbAddressService;
import com.guigu.qingcheng.utils.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
*
* @author Jiang
* @date 2020/08/05
*/
@Service
public class TbAddressServiceImpl implements TbAddressService {
    @Autowired(required = false)
    private TbAddressMapper tbAddressMapper;



    @Override
    public List<TbAddress> queryAll() {
        List<TbAddress> tbAddress = tbAddressMapper.selectAll();
            return tbAddress;
    }

    @Override
    public PageUtil<TbAddress> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbAddress> pages = (Page<TbAddress>) tbAddressMapper.selectAll();
        PageUtil<TbAddress> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbAddress> pageQueryByAll(Integer pageNum, Integer pageSize, TbAddress tbAddress) {

        Example example=new Example(TbAddress.class);
        Example.Criteria criteria = example.createCriteria();
        // 重新实现判断参数

//        if(!StringUtils.isEmpty(tbAddress.getId())){
//            criteria.andEqualTo("id",tbAddress.getId());
//        }
        if(!StringUtils.isEmpty(tbAddress.getUsername())){
           criteria.andLike("username","%"+tbAddress.getUsername()+"%");
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<TbAddress> pages = (Page<TbAddress>) tbAddressMapper.selectByExample(example);
        PageUtil<TbAddress> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbAddress(TbAddress tbAddress) {
        int insert = tbAddressMapper.insert(tbAddress);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbAddress(TbAddress tbAddress) {
        int i = tbAddressMapper.updateByPrimaryKeySelective(tbAddress);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbAddressMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbAddress findById(Integer id) {
        TbAddress tbAddress = tbAddressMapper.selectByPrimaryKey(id);
        return tbAddress;
    }

    @Override
    public List<TbAddress> findAddressList(String username) {
        Example example=new Example(TbAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);
        List<TbAddress> list = tbAddressMapper.selectByExample(example);
        return list;
    }

    @Override
    public boolean updateAddress(String username, Integer id) {
        Example example = new Example(TbAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);
        List<TbAddress> tbAddresses = tbAddressMapper.selectByExample(example);
        boolean b = false;
        for (TbAddress address:tbAddresses) {
            if (address.getId()==id){
                address.setIsDefault("1");
                int i = tbAddressMapper.updateByPrimaryKeySelective(address);
                if (i>0)
                    b = true;
            }else {
                address.setIsDefault("0");
                tbAddressMapper.updateByPrimaryKeySelective(address);
            }
        }
        return b;
    }
}
